<#
.Synopsis
	Opens the desired file(s) in UltraEdit.
    
.Description
    The file(s) you specify on the command line or the pipeline will be opened in UltraEdit.  We'll launch a new
    copy of the app, but only if necessary.  The pipeline supports many varieties of objects via my ConvertTo-FullPath
    script.
    
    Put this in your $profile:
    PS> . "c:\path\to\invoke-ultraedit.ps1"
    PS> set-alias ue invoke-ultraedit
    
.Parameter File
    Path to the file being opened.  Any object that contains such a path as a property named FullName, Path, etc. 
    will do.
        
.Example
    PS> # open foo.txt in UltraEdit
    PS> ue foo.txt
    
.Example
    PS> # open any project files that contain a HintPath element
    PS> dir -r -fil *csproj | ss hintpath | ue
        
.ReturnValue

.Link
	http://richardberg.net/blog/?p=59
    http://richardberg.net/blog/?p=57
    
.Notes
 NAME:      Invoke-UltraEdit.ps1
 AUTHOR:    COATUECAP\rberg
 LASTEDIT:  03/27/2009 17:24:02

#Requires -Version 2.0
#>
function Invoke-UltraEdit
{
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, ValueFromPipeline=$true)] 
        [psobject] $file
    )
  
    Begin
    {
        $ue = (get-itemproperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\uedit32.exe").'(default)'
        $exe = (dir $ue).basename
          
        # If UltraEdit isn't running yet, launching it will hang the console until it exits
        # Same deal if there's an elevated UltraEdit process running but our shell is not elevated
        Function HaveToStartNewEditor
        {
            $haveToStartNew = $true
          	# If there is at least one process whose handle we have permission to grab, we're ok.            
            get-process $exe -ea SilentlyContinue | 
    	      % { 
                   if ($_.Handle -ne $null) 
                       { $haveToStartNew = $false }
    		    }
            $haveToStartNew
        }
        while (HaveToStartNewEditor)
        { 
            invoke-item $ue
            sleep 3
        }
    }
   
    Process
    {
        # desired list = '"c:\foo bar.txt" "c:\foo baz.txt"'
        $fullPath = $file | convertto-fullpath
        $fileList = "$fileList `"$fullPath`""
    }
    
    End
    {
        # launch with option to use existing window
        & $ue /foi $fileList
    }
}